package com.sixth.system.service.impl;/**
 * @author 633yh
 * @version 1.0
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sixth.core.entity.DictType;
import com.sixth.core.vo.R;
import com.sixth.system.mapper.DictTypeMapper;
import com.sixth.system.service.DictTypeService;
import com.sixth.system.vo.DictTypeVo;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author     ：Stu YH
 * @date       ：Created in 2024/8/19 16:59
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class DictTypeServiceImpl implements DictTypeService {
    @Autowired
    private DictTypeMapper dictTypeMapper;
    @Override
    public R<Page<DictType>> selectByPage(DictTypeVo dictTypeVo) {
        Page<DictType> page = new Page<>(dictTypeVo.getPageNum(), dictTypeVo.getPageSize());
        QueryWrapper<DictType> wrapper = new QueryWrapper<>();
        if (dictTypeVo.getDictName() != null) {
            wrapper.like("dict_name", dictTypeVo.getDictName());
        }
        if (dictTypeVo.getDictType() != null) {
            wrapper.like("dict_type", dictTypeVo.getDictType());
        }
        if (dictTypeVo.getStatus() != null) {
            wrapper.eq("status", dictTypeVo.getStatus());
        }
        if (dictTypeVo.getDateRange() != null && dictTypeVo.getDateRange().size() == 2) {
            wrapper.between("create_time", dictTypeVo.getDateRange().get(0), dictTypeVo.getDateRange().get(1));
        }
        Page<DictType> dictTypePage = dictTypeMapper.selectPage(page, wrapper);
        return R.ok(dictTypePage);
    }

    @Override
    public boolean addUpdate(DictType dictType) {
        dictType.setCreateTime(new Date());
        boolean res = dictTypeMapper.insertOrUpdate(dictType);
        return res;
    }

    @Override
    public R batchDelete(Long[] dictIds) {
        List<DictType> collect = Arrays.stream(dictIds).map(item -> {
            DictType dictType = new DictType();
            dictType.setDictId(item);
            return dictType;
        }).collect(Collectors.toList());
        dictTypeMapper.deleteBatchIds(collect);
        return R.ok();
    }
}